package com.gmono.managers;

import java.sql.*;

public class User
{
    private String username;
    private String password;
    private String phonenumber;
    private String name;
    private int id;
    private Connection c=Common.c;
    public User(){}

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isValid()
    {
        //查询
        try {
            PreparedStatement s= c.prepareStatement("SELECT count(*) from users where username= ? and password= ?");
            s.setString(1,username);
            s.setString(2,password);
            ResultSet set=s.executeQuery();
            set.next();
            int count=set.getInt(1);
            return count!=0;
        } catch (SQLException e) {
            return false;
        }

    }
    public User getInfo()
    {
        try{
            PreparedStatement s= c.prepareStatement("SELECT * from users WHERE username=?");
            s.setString(1,username);
            ResultSet set=s.executeQuery();
            if(!set.next()) return null;
            fillObject(set);
            return this;
        }catch (SQLException e)
        {
            return null;
        }
    }
    public boolean isExists()
    {
        PreparedStatement s= null;
        try {
            s = c.prepareStatement("select count(*) from users where username=?");
            s.setString(1,username);
            ResultSet set=s.executeQuery();
            set.next();
            int e=set.getInt(1);
            return e!=0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }


    public String getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public static User getObject(ResultSet row)
    {
        User ret=new User();
        return ret.fillObject(row)? ret:null;
    }
    public boolean fillObject(ResultSet row)
    {
        try {
            User ret=this;
            ret.username=row.getString("username");
            ret.name=row.getString("name");
            ret.phonenumber=row.getString("phonenumber");
            ret.password=row.getString("password");
            ret.id=row.getInt("id");
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean update()
    {
        try
        {
            PreparedStatement s=c.prepareStatement("update users set username=?,name=?,phonenumber=?,password=? where id=?");
            s.setString(1,username);
            s.setString(2,name);
            s.setString(3,phonenumber);
            s.setString(4,password);
            s.setInt(5,id);
            s.executeUpdate();
            return true;
        }catch (Exception e)
        {
            return false;
        }
    }

}
